package gestionCandidats;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.SortedSet;
import java.util.TreeSet;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.BevelBorder;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableModel;

import modele.ComparatorDossierMoyenne;
import modele.ComparatorDossierNom;
import modele.Dossier;
import modele.Etudiant;

public class DossierRetenuAfficheAction 
extends AbstractAction  {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected String[] columnToolTips = {
			"<html>Le numero du dossier</html>",
			"<html>Le numero ApoFlux du dossier</html>",
			"<html>Entre crochets le nombre de modifications</html>",
			null,
			"<html>OUI = redoublant</html>",
			"<html>La provenance du candidat</html>",
			"<html>Le dossier est valide ssi dans l'ecran evaluation<ul><li>OK administratif</li><li>Bac+4 acquis</li><li>OK pedagogique</li></ul></html>",
			"<html>La note mise par l'evaluateur</html>",
			"<html>La liste ordonnee des specialites demandees<br>Entre crochets le nombre de modifications</html>",
			"<html>La liste ordonnee des propositions<br>Entre crochets le nombre de propositions faites</html>",
			"<html>La reponse a la derniere proposition</html>"
	};

	SortedSet<Dossier> lDos;
	GestionCandidats appli;
	JScrollPane scrollpane;
	JPanel vue;
	TableModel dataModel;
	JTable lDossier;
	JComboBox<String> triJCB;

	protected DossierRetenuAfficheAction(GestionCandidats appli) {
		super("DossierRetenuAfficheAction");
		this.appli = appli;
		vue = appli.getDossierRetenuAffiche();
	}	

	public JTable getDossierListe(){
		return lDossier;
	}

	private void trierDossierNom() {
		SortedSet<Dossier> aux = new TreeSet<Dossier>(new ComparatorDossierNom());
		for (Dossier dos : lDos)
			aux.add(dos);
		lDos = aux;
	}

	private void trierDossierNoteDossier() {
		SortedSet<Dossier> aux = new TreeSet<Dossier>(new ComparatorDossierMoyenne());
		for (Dossier dos : lDos)
			aux.add(dos);
		lDos = aux;
	}

	private ActionListener trierDossierAction
	(final DossierRetenuAfficheAction draa) {
		ActionListener trierDossierListener = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					draa.actionPerformed(e);
				}
				catch (Exception ex){
					JOptionPane.showMessageDialog(appli, ex.getMessage(), "Tri dossiers : Attention",JOptionPane.ERROR_MESSAGE);
					ex.printStackTrace();
				}
			}
		};
		return trierDossierListener;
	}

	private Dossier getSelectedDossier(Object ident) throws Exception {
		for (Dossier dos : lDos) {
			if (dos.getEtudiant().getIdApoflux().equals((String)ident))
				return dos;
		}
		return null;
	}

	public Dossier getSelectedDossier() throws Exception {
		if (getDossierListe().getSelectedRow()!=-1)
			return getSelectedDossier(dataModel.
					getValueAt(getDossierListe().
							getSelectedRow(),0));
		else
			return null;
	}

	public Component getVue(){
		return vue;
	}

	public void actionPerformed(ActionEvent e) {
		try {
			if(vue == null) {
				vue = new JPanel();
				vue.setLayout(new BorderLayout());
				appli.add(vue,BorderLayout.CENTER);
				scrollpane = new JScrollPane();
				scrollpane.setBorder(new BevelBorder(BevelBorder.LOWERED));
				vue.add(scrollpane,BorderLayout.CENTER);

				JPanel pied = new JPanel();
				vue.add(pied, BorderLayout.SOUTH);
				triJCB = new JComboBox<String>();
				triJCB.addItem("Tri par numero");
				triJCB.addItem("Tri par nom");
				triJCB.addItem("Tri par note de dossier");
				triJCB.setSelectedIndex(0);
				triJCB.addActionListener(trierDossierAction(this));
				pied.add(triJCB);
				JButton retour = new JButton("Fermer");
				retour.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						vue.setVisible(false);
					}
				});
				pied.add(retour);
			}
			lDos = appli.getBd().getDossiersRetenus();
			if (triJCB.getSelectedIndex() == 1)
				trierDossierNom();
			else if (triJCB.getSelectedIndex() == 2)
				trierDossierNoteDossier();
			final String[] names = {"Apoflux", "Nom", "Prenom", "Redoublant", "Origine", "Note", "Parcours"};
			final Object[][] data = new Object[lDos.size()][names.length];
			int i = 0;
			for (Dossier dos : lDos) {
				Etudiant etu = dos.getEtudiant();
				data[i][0] = etu.getIdApoflux();
				data[i][1] = etu.getNom();
				data[i][2] = etu.getPrenom();
				data[i][3] = etu.getRedoublant();
				data[i][4] = etu.getEtablissementOrigine();
				data[i][5] = dos.getNoteDossier();
				data[i][6] = dos.getPropositionParcours();
				i++;
			}
			// Create a model of the data.
			dataModel = new AbstractTableModel() {
				/**
				 * 
				 */
				private static final long serialVersionUID = 1L;
				// These methods always need to be implemented.
				public int getColumnCount() { return names.length; }
				public int getRowCount() { return lDos.size();}
				public Object getValueAt(int row, int col) {
					return data[row][col];
				}
				public void setValueAt(Object aValue, int row, int column){
					data[row][column] = aValue;
				}
				public String getColumnName(int column) {
					return names[column];
				}
				@SuppressWarnings({ "unchecked", "rawtypes" })
				public Class getColumnClass(int c) {
					return getValueAt(0, c).getClass();
				}
			};
			// Create the table
			lDossier = new JTable(dataModel){
				/**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				protected JTableHeader createDefaultTableHeader(){
					return new JTableHeader(columnModel) {
						/**
						 * 
						 */
						private static final long serialVersionUID = 1L;

						public String getToolTipText(MouseEvent e){
						//	String tip = null;
							java.awt.Point p = e.getPoint();
							int index = columnModel.
							getColumnIndexAtX(p.x);
							int realIndex = columnModel.
							getColumn(index).
							getModelIndex();
							return columnToolTips[realIndex];
						}
					};
				}
			};
			lDossier.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
			lDossier.setRowHeight(2*lDossier.getRowHeight());
			// Finish setting up the table.

//			JComboBox origineJCB = new JComboBox();
			//origineJCB.addItem("Master 1 Informatique Bordeaux 1");
			// modif JS 17/06/08: preciser specialite
//			origineJCB.addItem("Master 1 Informatique Bordeaux 1 - AMF");
//			origineJCB.addItem("Master 1 Informatique Bordeaux 1 - GL");
//			origineJCB.addItem("Master 1 Informatique Bordeaux 1 - ISM");
//			origineJCB.addItem("Master 1 Informatique Bordeaux 1 - SR");
//			origineJCB.addItem("Master 1 Informatique Bordeaux 1 - CSI");
//
//			origineJCB.addItem("Master 1 Informatique");
//			origineJCB.addItem("Autre Master 1 Bordeaux 1");
//			origineJCB.addItem("Diplome Francais");
//			origineJCB.addItem("Diplome Non Francais");
//			TableColumn origineTC = lDossier.getColumn(names[4]);
//			origineTC.setCellEditor(new DefaultCellEditor(origineJCB));

			scrollpane.setViewportView(lDossier);
			vue.setVisible(true);
			appli.getFrame().setVisible(true);
		}
		catch(Exception ex){
			JOptionPane.showMessageDialog(appli, ex.getMessage(), "Affichage des dossiers : Attention",JOptionPane.ERROR_MESSAGE);
			ex.printStackTrace();
		}
	}
}

